using Comteck.Domain.Pages;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Comteck.Winforms.Controls {

  /// <summary>
  /// 查询事件
  /// </summary>
  /// <param name="sender">GridView Control</param>
  /// <param name="e">FocusedRowChangedEventArgs</param>
  public delegate void UcDataNavigatorEventHandler();

  /// <summary>
  /// 自定义分页
  /// </summary>
  public partial class UcDataNavigator : UcBase {

    #region 自定义事件

    /// <summary>
    /// 修改下拉框值后事件
    /// </summary>
    private static readonly object EventUcDataNavigatorPaged = new object();

    /// <summary>
    /// 修改下拉框值前事件
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
    [Category("扩展功能"), Description("修改下拉框值前事件")]
    public event UcDataNavigatorEventHandler UcDataNavigatorPaged {
      add {
        this.Events.AddHandler(EventUcDataNavigatorPaged, value);
      }
      remove {
        this.Events.RemoveHandler(EventUcDataNavigatorPaged, value);
      }
    }

    #endregion

    #region fields

    private static string CUSTOMERPAGESIZE_TEXT = "其他";

    private static int CUSTOMERPAGESIZE_MAX = 10000;

    /// <summary>
    /// 
    /// </summary>
    private int MaxPage = 1;

    #endregion

    #region Methods

    /// <summary>
    /// 初始化分页信息
    /// </summary>
    /// <param name="model"></param>
    public bool GetPageData(IPageModel model) {
      // 分页大小
      var pageSize = this.barPageSize.EditValue.ToString();


      if (CUSTOMERPAGESIZE_TEXT.Equals(pageSize)) {
        var customerSize = barPageSizeCustom.EditValue.ToString(true).Trim();

        if (string.IsNullOrWhiteSpace(customerSize)) {
          XtraMessageBox.Show("请输入自定义分页大小！");
          return false;
        }

        model.Size = Convert.ToInt32(customerSize);

        if (model.Size > CUSTOMERPAGESIZE_MAX) {
          var dialog = XtraMessageBox.Show("您查询的分页大小超过 {0} 条，可能比较慢，您确定要查询吗？".FormatWith(CUSTOMERPAGESIZE_MAX), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

          if (dialog == DialogResult.No) {
            return false;
          }
        }
      } else {
        model.Size = Convert.ToInt32(pageSize);
      }

      // 页码
      var page = (this.barCurrPage.EditValue ?? string.Empty).ToString().Trim();

      if (String.IsNullOrWhiteSpace(page)) {
        XtraMessageBox.Show("请输入页码！");
        return false;
      }

      model.Page = Convert.ToInt32(page);

      return true;
    }

    /// <summary>
    /// 初始化分页信息
    /// </summary>
    /// <param name="model"></param>
    public void InitPageData(IPageModel model) {
      this.barCurrPage.EditValue = model.Page;
      this.barPageFormat.Caption = this.barPageFormat.Tag.ToString().FormatWith(model.PageCount);
      this.barSummaryFormat.Caption = this.barSummaryFormat.Tag.ToString().FormatWith(model.Total, model.Page);
      this.MaxPage = model.PageCount;
    }

    /// <summary>
    /// 初始化分页信息
    /// </summary>
    public void ClearPageData() {
      this.barCurrPage.EditValue = 1;
      this.barPageFormat.Caption = this.barPageFormat.Tag.ToString().FormatWith(1);
      this.barSummaryFormat.Caption = this.barSummaryFormat.Tag.ToString().FormatWith(0, 1);
      this.MaxPage = 1;
    }

    private void SearchData() {
      if (this.Events[EventUcDataNavigatorPaged] is UcDataNavigatorEventHandler handler) {
        handler();
      }
    }

    #endregion

    #region ctor

    /// <summary>
    /// 
    /// </summary>
    public UcDataNavigator() {
      InitializeComponent();

      this.barPageFormat.Tag = this.barPageFormat.Caption;
      this.barSummaryFormat.Tag = this.barSummaryFormat.Caption;

      this.barPageFormat.Caption = this.barPageFormat.Tag.ToString().FormatWith(this.MaxPage);
      this.barSummaryFormat.Caption = this.barSummaryFormat.Tag.ToString().FormatWith(0, 1);
    }

    #endregion

    #region 窗口加载

    /// <summary>
    /// 窗口加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void UcDataNavigator_Load(object sender, EventArgs e) {

      if (this.DesignMode) {
        return;
      }

    }

    #endregion

    #region 自定义分页大小

    /// <summary>
    /// 下拉框改变
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void barPageSize_EditValueChanged(object sender, EventArgs e) {
      var value = this.barPageSize.EditValue.ToString();

      if (CUSTOMERPAGESIZE_TEXT.Equals(value)) {
        this.barPageSizeCustom.Enabled = true;
      } else {
        this.barPageSizeCustom.EditValue = null;
        this.barPageSizeCustom.Enabled = false;

        this.SearchData();
      }
    }

    #endregion

    #region 分页相关事件

    /// <summary>
    /// 分页大小回车时间
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void rpPageSizeCustom_KeyDown(object sender, KeyEventArgs e) {
      if (e.KeyCode == Keys.Enter) {
        this.barPageSizeCustom.EditValue = (sender as TextEdit).EditValue.ToString();

        this.SearchData();
      }
    }

    /// <summary>
    /// 第一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnFirstPage_ItemClick(object sender, ItemClickEventArgs e) {
      this.barCurrPage.EditValue = 1;

      this.SearchData();
    }

    /// <summary>
    /// 前一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnPrevPage_ItemClick(object sender, ItemClickEventArgs e) {
      var page = (this.barCurrPage.EditValue ?? string.Empty).ToString().Trim().To<int>();

      this.barCurrPage.EditValue = Math.Max(page - 1, 1);

      this.SearchData();
    }

    /// <summary>
    /// 页码回车事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void rpCurrPage_KeyDown(object sender, KeyEventArgs e) {
      if (e.KeyCode == Keys.Enter) {
        this.barCurrPage.EditValue = (sender as TextEdit).EditValue.ToString();

        this.SearchData();
      }

    }

    /// <summary>
    /// 下一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnNextPage_ItemClick(object sender, ItemClickEventArgs e) {
      var page = (this.barCurrPage.EditValue ?? String.Empty).ToString().Trim().To<int>();

      // modify by geping@2019-03-28
      //this.barCurrPage.EditValue = Math.Max(page + 1, 1);
      this.barCurrPage.EditValue = Math.Min(page + 1, this.MaxPage);

      this.SearchData();
    }

    /// <summary>
    /// 最后一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnLastPage_ItemClick(object sender, ItemClickEventArgs e) {
      this.barCurrPage.EditValue = this.MaxPage;

      this.SearchData();
    }

    /// <summary>
    /// 刷新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnRefresh_ItemClick(object sender, ItemClickEventArgs e) {
      this.SearchData();
    }

    #endregion

    #region 全选

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void rpPageSizeCustom_Click(object sender, EventArgs e) {
      var edit = sender as TextEdit;
      edit.SelectAll();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void rpCurrPage_Click(object sender, EventArgs e) {
      var edit = sender as TextEdit;
      edit.SelectAll();
    }

    #endregion
  }
}
